package com.ruike.alisurface.ui.QuHuoMa;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.ShopDetailBean;
import com.ruike.alisurface.http.DouShanShanHttp;
import com.ruike.alisurface.ui.payPage.PayShowShopActivity;
import com.ruike.alisurface.utils.GlideImageLoader;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.ruike.alisurface.Constant.BG_IMG_PATH;
import static com.ruike.alisurface.Constant.INPUT_FILE_NAME;

public class InputCodeActivity extends BaseAty {

    @BindView(R.id.inputCode_toLogin_view)
    View toLoginView;
    @BindView(R.id.inputCode_goBack_fl)
    FrameLayout goBackFl;
    @BindView(R.id.inputCode_bg_imgv)
    ImageView bgImgv;
    @BindView(R.id.inputCode_code_tv)
    TextView codeTv;
    @BindView(R.id.inputCode_submit_btn)
    Button submitBtn;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_input_code;
    }

    @Override
    protected void initViews() {
        // 加载背景图
        GlideImageLoader.loadImage(this, BG_IMG_PATH + INPUT_FILE_NAME, bgImgv);
    }

    @Override
    protected void initData() {
        if (ShareUtils.getInstance().getInt(ShareKey.ADV_TYPE) == Constant.ADV_TYPE_NONE) {
            // 无广告
            goBackFl.setVisibility(View.INVISIBLE);
            toLoginView.setOnClickListener(view -> {
                continuousClick();
            });
        } else {
            goBackFl.setVisibility(View.VISIBLE);
            startCountdownTimer(300, null, null);
        }
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    @OnClick({R.id.inputCode_goBack_fl,
            R.id.inputCode_number1_btn, R.id.inputCode_number2_btn, R.id.inputCode_number3_btn,
            R.id.inputCode_number4_btn, R.id.inputCode_number5_btn, R.id.inputCode_number6_btn,
            R.id.inputCode_number7_btn, R.id.inputCode_number8_btn, R.id.inputCode_number9_btn,
            R.id.inputCode_delete_btn, R.id.inputCode_number0_btn, R.id.inputCode_submit_btn})
    public void onViewClicked(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.inputCode_goBack_fl:
                startCountdownTimer(0, null, null);
                break;
            case R.id.inputCode_number0_btn:
            case R.id.inputCode_number1_btn:
            case R.id.inputCode_number2_btn:
            case R.id.inputCode_number3_btn:
            case R.id.inputCode_number4_btn:
            case R.id.inputCode_number5_btn:
            case R.id.inputCode_number6_btn:
            case R.id.inputCode_number7_btn:
            case R.id.inputCode_number8_btn:
            case R.id.inputCode_number9_btn:
                inputNumber(viewId == R.id.inputCode_number0_btn ? 0 :
                        viewId == R.id.inputCode_number1_btn ? 1 :
                                viewId == R.id.inputCode_number2_btn ? 2 :
                                        viewId == R.id.inputCode_number3_btn ? 3 :
                                                viewId == R.id.inputCode_number4_btn ? 4 :
                                                        viewId == R.id.inputCode_number5_btn ? 5 :
                                                                viewId == R.id.inputCode_number6_btn ? 6 :
                                                                        viewId == R.id.inputCode_number7_btn ? 7 :
                                                                                viewId == R.id.inputCode_number8_btn ? 8 : 9
                );
                break;
            case R.id.inputCode_delete_btn:
                deleteNumber();
                break;
            case R.id.inputCode_submit_btn:
                submit();
                break;
        }
    }

    public void inputNumber(int number) {
        String currentNumber = codeTv.getText().toString().trim();
        codeTv.setText(String.format(Locale.ENGLISH, "%s%d", currentNumber, number));
    }

    public void deleteNumber() {
        String currentNumber = codeTv.getText().toString().trim();
        codeTv.setText(TextUtils.isEmpty(currentNumber) ? "" : currentNumber.substring(0, currentNumber.length() - 1));
    }

    public void submit() {
        String currentNumber = codeTv.getText().toString().trim();
        if (TextUtils.isEmpty(currentNumber)) {
            showErrorTip("请输入兑换码");
            return;
        }
        showProgressDialog("正在上传取货码");
        DouShanShanHttp.getOut_goods_code(currentNumber, this);
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        if (requestUrl.contains("Android_StartOut")) {
            outGoods(requestJsonStr);
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
        showErrorTip(errorMsg);
        codeTv.setText("");
    }

    private void outGoods(String requestJsonStr) {
        try {
            JSONObject jsonObject = new JSONObject(requestJsonStr);
            JSONObject dataJson = jsonObject.optJSONObject("data");
            String codeId = dataJson.optString("codeid");
            JSONArray outInfoArray = dataJson.optJSONArray("out_info");

            ShopDetailBean shopDetailBean = new ShopDetailBean(codeId);

            List<ShopDetailBean.OrderDetailsBean> orderDetailsBeanList = new ArrayList<>();
            if (outInfoArray != null) {
                for (int i = 0; i < outInfoArray.length(); i++) {
                    ShopDetailBean.OrderDetailsBean orderDetailsBean = new ShopDetailBean.OrderDetailsBean();
                    JSONObject infoJson = outInfoArray.getJSONObject(i);
                    orderDetailsBean.setSlotIndex(infoJson.optInt("index"));
                    orderDetailsBean.setQuantity(1);
                    orderDetailsBean.setSlotTypeId(infoJson.optInt("slotTypeId"));
                    orderDetailsBeanList.add(orderDetailsBean);
                }
            }

            shopDetailBean.setDetailOrderList(orderDetailsBeanList);

            Bundle bundle = new Bundle();
            bundle.putString("pay_type", "QuHuoMa");
            bundle.putSerializable("orderBeanData", shopDetailBean);
            startActivity(PayShowShopActivity.class, bundle);
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
